<?php

use yii\helpers\Url;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
use yii\widgets\Pjax;
use common\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model backend\modules\admin\models\User */

$this->title = '查看用户';
$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="user-view">

    <div class="panel">
		<div class="panel-heading">
            <?= Html::encode($this->title) ?>
            <div class="pull-right">
                <?= Html::a('修改', ['update', 'id' => $model->id], ['class' => 'btn btn-sm btn-cyan']) ?>
                <?= Html::a('删除', ['delete', 'id' => $model->id], [
                    'class' => 'btn btn-sm btn-danger',
                    'data' => [
                        'confirm' => 'Are you sure you want to delete this item?',
                        'method' => 'post',
                    ],
                ]) ?>
                <?= Html::a('个人资料', ['person/view', 'id' => $model->person_id], ['class' => 'btn btn-sm btn-cyan']) ?>
            </div>
		</div>
        <div class="panel-body">
            <?= DetailView::widget([
                'model' => $model,
                'fieldRows' => [
                    [
                        'ID' => $model->id,
                        '认证密钥' => $model->auth_key,
                    ],[
                        '用户名' => $model->username,
                        '密码密文' => $model->password_hash,
                    ],[
                        '邮箱' => $model->email,
                        '密码重置令牌' => $model->password_reset_token,
                    ],[
                        '邮箱验证令牌' => $model->verification_token,
                        '状态' => ([
                                \backend\modules\admin\models\User::STATUS_ACTIVE => '启用',
                                \backend\modules\admin\models\User::STATUS_INACTIVE => '禁用',
                                \backend\modules\admin\models\User::STATUS_DELETED => '已删除',
                            ])[$model->status],
                    ],[
                        '最后登录时间:datetime' => $model->last_login_time,
                        '创建时间:datetime' => $model->created_at,
                    ],[
                        '最后登录IP' => $model->last_login_ip,
                        '修改时间:datetime' => $model->updated_at,
                        //'用户名' => $model->person_id,
                    ]
                ],
            ]) ?>
        </div>
    </div>

    <div class="panel">
		<div class="panel-heading">
            用户的角色与权限
            <div class="pull-right">
                <?= Html::a('添加角色', ['auth', 'id' => $model->id], ['class' => 'btn btn-auth-role btn-sm btn-cyan']) ?>
                <?= Html::a('添加权限', ['auth', 'id' => $model->id], ['class' => 'btn btn-auth-perm btn-sm btn-cyan']) ?>
                <?= Html::beginForm(['auth', 'id' => $model->id], 'post', ['class'=>'auth-form']) ?>
                <?= Html::endForm() ?>
            </div>
		</div>
        <div class="panel-body">
            <?= GridView::widget([
                'layout' => "{items}\n{pager}",
                'tableOptions' => ['class' => 'table table-hover table-bordered'],
                'dataProvider' => new ActiveDataProvider(['query' => $model->getAuthItems()]),
                //'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    [
                        'attribute' => 'name',
                        'label' => '名称',
                    ],

                    [
                        'attribute' => 'type',
                        'label' => '类型',
                        'value' => function($item){ return $item->type===1?'角色':'权限'; },
                    ],

                    [
                        'class' => 'yii\grid\ActionColumn',
                        'template' => '{delete}',
                        'contentOptions' => ['class'=>'text-right'],
                        'buttons' => [
                            'delete' => function ($url, $item, $key)use($model) {
                                return Html::a('撤销',
                                    ['un-auth', 'id'=>$model->id, 'item_name'=>$item->name],
                                    [
                                        'class' => 'btn btn-xs btn-danger',
                                        'data' => [
                                            'confirm' => '您确定要撤销此项授权吗？',
                                            'method' => 'post',
                                        ]
                                    ]
                                );
                            },
                        ],
                    ],
                ],
            ]); ?>
        </div>
    </div>

    <?php $this->render('//common/dialog'); ?>

    <script>
    <?php $this->beginBlock('auth-item-form-js') ?>
        // 为用户添加授权
        function addAuthItems(type, items){
            var $form = $('.auth-form');   
            $form.append('<input type="hidden" name="type" value="'+type+'"/>');         
            items.forEach(item=>{
                $form.append('<input type="hidden" name="items[]" value="'+item+'"/>');
            });
            $form.submit();  // 提交表单
        }

        // 选择角色，添加授权
        $('.btn-auth-role').on('click', function(){
            commonDialog('/admin/role/list', {title: '选择角色'}, data=>addAuthItems('role',data));
            return false;
        });

        // 选择权限，添加授权
        $('.btn-auth-perm').on('click', function(){
            commonDialog('/admin/permission/list', {title: '选择权限'}, data=>addAuthItems('permission',data));
            return false;
        });
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['auth-item-form-js']);?>
    </script>
</div>
